<template>
  <div class="app-container order cloud-order">
    <!-- 工具栏 -->
    <div class="head-container">
      <div class="head-container-tools">
        <template v-if="crud.props.searchToggle">
          <span>选择日期: </span>
          <el-date-picker
            v-model="query.startTime"
            type="date"
            size="small"
            class="filter-item charges-date-picker"
            placeholder="开始日期"
            value-format="yyyy-MM-dd"
            style="width: 140px"
          >
          </el-date-picker>
          <span> 至  </span>
          <el-date-picker
            v-model="query.endTime"
            type="date"
            size="small"
            class="filter-item charges-date-picker"
            placeholder="结束日期"
            value-format="yyyy-MM-dd"
            style="width: 140px"
          >
          </el-date-picker>
          <el-input
            v-model="query.username"
            placeholder="开票人姓名"
            style="width: 160px"
            class="filter-item"
            size="small"
          />
        </template>
        <rrOperation :crud="crud" @resetQuery="onResetQuery" />
      </div>
    </div>
    <crudOperation :permissions="permission" />
    <el-table
      v-loading="crud.loading"
      :data="crud.data"
      ref="table"
    >
      <el-table-column  label="姓名" prop="username"></el-table-column>
      <el-table-column  label="收费项目" prop="project"></el-table-column>
      <el-table-column  label="收费金额" prop="money"></el-table-column>
      <el-table-column  label="收款人" prop="collectionUser"></el-table-column>
      <el-table-column  label="开票员" prop="createUserName" ></el-table-column>
      <el-table-column  label="开票时间" prop="openTime" ></el-table-column>
      <el-table-column  label="打印次数" prop="printingTimes" ></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <template v-for="item in scope.row.btns">
            <el-button
              :key="item.id"
              type="text"
              @click="onRowHandle(item)"
            >{{ item.text }}</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination />
       <!-- 弹窗 -->
    <el-dialog
      append-to-body
      top="10vh"
      :title="form.id.length> 0? '编辑收据':'新增收据'"
      :close-on-click-modal="false"
      :visible.sync="showDialog"
    >
       <el-form
      label-width="100px"
      size="small"
      ref="form"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="开票人姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入开票人姓名" />
      </el-form-item>
            <el-form-item label="收费项目" prop="project">
        <el-input v-model="form.project" placeholder="请输入收费项目" />
      </el-form-item>
            <el-form-item label="收费金额" prop="money">
        <el-input v-model="form.money" placeholder="请输入收费金额" />
      </el-form-item>
            <el-form-item label="开票时间" prop="openTime">
         <el-date-picker
          v-model="form.openTime"
          type="date"

          size="small"
          class="filter-item charges-date-picker"
          placeholder="开票时间"
          value-format="yyyy-MM-dd"
          style="width: 240px"
        >
        </el-date-picker>
      </el-form-item>
            <el-form-item label="收款人" prop="collectionUser">
        <el-input v-model="form.collectionUser" placeholder="请输入收款人" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="showDialog = !showDialog">取消</el-button>
      <el-button :loading="crud.procesLoading" type="primary" @click="onClickSumbit">确认</el-button>
    </div>
    </el-dialog>
    <!-- 票据打印... -->
    <printSend ref="printSend"></printSend>
  </div>
</template>

<script>
import { getPatientSex, formatMoney } from "@/utils/index";
import CRUD, { presenter, header, form } from "@crud/crud";
import crudOperation from '@crud/CRUD.operation';
import rrOperation from "@crud/RR.operation";
import printSend from "./module/print";
import pagination from "@crud/Pagination";
import { add ,update ,updatePrintTimes} from "@/api/charges/invoice";

let defaultCrud = CRUD({
  query: { endTime: null, startTime: null, pharmacyId: null, sort: 'asc',
           username: null },
  hideColumns: [],
  url: "api/invoice/getList",
  sort: [],
  title: "发票管理"
});

let defaultForm = {
  collectionUser: "",
  createTime: '',
  openTime:'',
  createUserId:'',
  createUserName:'',
  money: 0,
  pharmacyId: '',
  project:'',
  username:null,
  id:'',
  createUserName :'',
  createUserId : ''
}

export default {
  name: "invoice",
  mixins: [ presenter(defaultCrud), header() ,form(defaultForm)],
  components: {
    rrOperation,
    pagination,
    printSend,
    crudOperation,
  },
  data() {
    return {
      permission: [
        {
          type: "add",
          name: "新增收据",
          position: 1,
          permission: ['admin', 'invoice:add'],
          handleClick: "onAdd"
        }
      ],
      rules: {
        username: [
          { required: true, message: "请输入开票人名称", trigger: "blur" },
        ],
        project: [
          { required: true, message: "请输入收费项目", trigger: "blur" },
        ],
        money: [
          { required: true, message: "请输入收费金额", trigger: "blur" },
        ],
        openTime: [
          { required: true, message: "请选择开票时间", trigger: "blur" },
        ],
        collectionUser: [
          { required: true, message: "请输入收款人", trigger: "blur" },
        ]
      },
      operation:0,
      flag: 1,
      showDialog: false,
    }
  },
  created() {
    this.$store.dispatch("GetInfo").then(res => {
      this.query.pharmacyId = res.pharmacyId;
      this.crud.toQuery();
    });
  },
  mounted() {
    if(this.query.startTime && this.query.endTime) {
      this.defaultDate = [
        this.query.startTime,
        this.query.endTime
      ]
    }
  },
  methods: {
    onClickSumbit(){
      if(this.form.username == null||this.form.collectionUser == null||this.form.openTime == null||this.form.money == null||this.form.project == null){

        this.$message({
          showClose: true,
          message: '请完善开票信息',
          type: 'error'
        });

      }else{
        this.showDialog = !this.showDialog;
        if(this.form.id > 0){
             update(this.form).then(res => {
        this.data = res;
        this.crud.toQuery();
      });
        }else{
     add(this.form).then(res => {
        this.data = res;
        this.crud.toQuery();
      });
        }


      this.crud.toQuery();
      }

    },
    getPatientSex, formatMoney,
    [CRUD.HOOK.dataLoadSuccess](crud, data) {
      data.forEach(item => {
        item.btns = this._operateBtns(item);
      });
    },
    [CRUD.HOOK.afterRefresh]() {

    },
    onAdd() {
      this.operation = 0;
    //   this.warehousingId = null;
    this.form.username = null;
            this.form.id = '';
            this.form.project = null;
            this.form.printingTimes = null;
            this.form.pharmacyId = '';
            this.form.openTime = null;
            this.form.money = null;
            this.form.collectionUser = null;
            this.form.createUserName = '';
            this.form.createUserId = '';
      this.showDialog = true;

    },
    onOperateRow(id,operate) {
        if(id == 1){
            this.showDialog = true;
            this.form.username = operate.username;
            this.form.id = operate.id;
            this.form.project = operate.project;
            this.form.printingTimes = operate.printingTimes;
            this.form.pharmacyId = operate.pharmacyId;
            this.form.openTime = operate.openTime;
            this.form.money = operate.money;
            this.form.collectionUser = operate.collectionUser;
            this.form.createUserName = operate.createUserName;
            this.form.createUserId = operate.createUserId;
        }else if(id == 6){
             updatePrintTimes(operate.id).then(res => {
        // this.data = res;
        this.crud.toQuery();
        this.$refs.printSend.open(operate);
      });

        }
    },
    onResetQuery() {
      this.defaultDate = null;
    },
    onRowHandle(btn) {
      btn.handle.apply(this, btn.params)
    },
    _operateBtns(item) {
      let res = [];
      res.push({ text: "编辑", handle: this.onOperateRow, params: [1,item] });
    res.push({ text: "打印预览", handle: this.onOperateRow, params: [6,item] });

      return res;
    },
    _success() {
      this.$notify({
        title: "操作成功",
        type: "success"
      });
      this.crud.toQuery();
    }
  }
}
</script>
